package popcorn.ticket.controller;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import popcorn.ticket.entity.TypeClass;
import popcorn.ticket.entity.TypeClassExample;
import popcorn.ticket.service.TypeClassService;
import popcorn.ticket.tool.AjaxResponse;

@Controller
public class TypeClassController {

	@Autowired
	TypeClassService tcs;
	
	Logger logger = Logger.getLogger(TypeClassController.class);
	
	@RequestMapping("typeByMid/{movieid}")
	@ResponseBody
	public List<TypeClass> getTypeByMid(@PathVariable("movieid") int movieid){
		TypeClassExample example = new TypeClassExample();
		example.createCriteria().andMovieidEqualTo(movieid);
		return tcs.selectByExample(example);
	}
	String msg = "失败";
	int code = 0;
	@RequestMapping("typeclass")
	@ResponseBody
	public AjaxResponse AddMovieType(@RequestBody List<Integer> list) {
		TypeClass record = new TypeClass();
		int movieid = list.get(0);
		record.setMovieid(movieid);
		TypeClassExample example = new TypeClassExample();
		example.createCriteria().andMovieidEqualTo(movieid);
		List<TypeClass> typelist = tcs.selectByExample(example);
		List<Integer> mylist = new ArrayList<Integer>();
		for (int i = 1; i < list.size(); i++) {
			mylist.add(list.get(i));
		}
		logger.info("list:"+list);
		logger.info("mylist:"+mylist);
		
		// 123 --  124   3d 4a
		
		// 原来没有 新加的
		if(typelist.size() == 0) {
			boolean flag0 = false;
			for (int i = 1; i < list.size(); i++) {
				record.setTypeid(list.get(i));
				flag0 = tcs.insert(record) > 0;
			}
			if(flag0) {
				msg = "选择成功!";
				code = 200;
				return ar(msg, code);
			}else {
				msg = "选择中出现错误";
				code = 0;
				return ar(msg, code);
			}
		}

		// 取出原来有的编号
		List<Integer> getlist = new ArrayList<Integer>();
		for (int i = 0; i < typelist.size(); i++) {
			getlist.add(typelist.get(i).getTypeid());
		}
		logger.info("getlist:"+getlist);
		

		// 现在有原来也有的
		List<Integer> oldlist = new ArrayList<Integer>();
		
		for (int i = 0; i < typelist.size(); i++) {
			for (int j = 0; j < mylist.size(); j++) {
				if(typelist.get(i).getTypeid() == mylist.get(j)) {
					// 获取原来有现在也有的
					oldlist.add(mylist.get(j));
				}
			}
		}
		logger.info("old:"+oldlist+","+oldlist.size());

		// 如果没有和原来一致的 先把原来的全删了 然后再把新的加进去
		boolean flag1 = false;
		boolean flag2 = false; 
		if(oldlist.size() == 0) {
			for (int i = 0; i < typelist.size(); i++) {
				TypeClassExample example2 = new TypeClassExample();
				example2.createCriteria().andTypeidEqualTo(typelist.get(i).getTypeid());
				flag1 = tcs.deleteByExample(example2) > 0;
			}
			for (int i = 0; i < mylist.size(); i++) {
				record.setTypeid(mylist.get(i));
				flag2 = tcs.insert(record) > 0;
			}
			if(flag1 && flag2) {
				msg = "选择成功!";
				code = 200;
				return ar(msg, code);
			}else {
				msg = "选择中出现错误";
				code = 0;
				return ar(msg, code);
			}
		}

		boolean flag3 = false;
		boolean flag4 = false;
		if(oldlist.size() == (list.size()-1)) {
			// 说明要删除原来有的 现在没有的
			getlist.removeAll(oldlist);
			
			logger.info("删除原来有的");
			logger.info("removelist:"+getlist);
			for (int i = 0; i < getlist.size(); i++) {
				TypeClassExample example2 = new TypeClassExample();
				example2.createCriteria().andTypeidEqualTo(getlist.get(i));
				logger.info("id："+getlist.get(i));
				flag3 = tcs.deleteByExample(example2) > 0;
			}
			if(flag3) {
				msg = "选择成功!";
				code = 200;
				return ar(msg, code);
			}else {
				msg = "选择中出现错误";
				code = 0;
				return ar(msg, code);
			}
		}else if(oldlist.size() < (list.size()-1)) {
			// 说明要增加原来没有 现在有的 
			if(oldlist.size() < getlist.size()) {
				getlist.removeAll(oldlist);
				for (int i = 0; i < getlist.size(); i++) {
					TypeClassExample example2 = new TypeClassExample();
					example2.createCriteria().andTypeidEqualTo(getlist.get(i));
					tcs.deleteByExample(example2);
				}
			}
			mylist.removeAll(oldlist);
			logger.info("增加原来没有的");
			logger.info("removelist:"+mylist);
			for (int i = 0; i < mylist.size(); i++) {
				record.setTypeid(mylist.get(i));
				flag4 = tcs.insert(record) > 0;
			}
			if(flag4) {
				msg = "选择成功!";
				code = 200;
				return ar(msg, code);
			}else {
				msg = "选择中出现错误";
				code = 0;
				return ar(msg, code);
			}
		}
		
		
		return ar(msg, code);
	}
	
	public AjaxResponse ar(String msg,int code) {
		AjaxResponse ar = new AjaxResponse(msg, code);
		return ar;
	}
}
